package com.example.lab2.controller;

import com.example.lab2.model.Book;
import com.example.lab2.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("book")
public class BookController extends BaseController{
    private BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }
    @RequestMapping("signUp")
    private String signUp(Book book){
        bookService.create(book);
        session.setAttribute("books",bookService.queryAll());
        return "redirect:/user/user.jsp";
    }
    @GetMapping("removeById/{id}")
    private String removeById(@PathVariable("id")int id){
        bookService.removeById(id);
        session.setAttribute("books",bookService.queryAll());
        return "redirect:/user/user.jsp";
    }
    @GetMapping("modifyById/{id}")
    private String modifyById(@PathVariable("id")int id){
        session.setAttribute("book", bookService.queryById(id));
        return "redirect:/book/modify.jsp";
    }
    @RequestMapping("modify")
    private String modify(Book book){
        bookService.modify("modifyById",book);
        session.setAttribute("books",bookService.queryAll());
        return "redirect:/user/user.jsp";
    }
    @GetMapping("queryDetail/{id}")
    private String queryDetail(@PathVariable("id") int id){
        Book book=bookService.queryById(id);
        return "redirect:/user/detail.jsp";

    }
}
